package com.qst.controller;

import com.qst.Constant;
import com.qst.ExamException;
import com.qst.entity.TestPersonnel;
import com.qst.entity.User;

import com.qst.service.TestPersonnelService;
import com.qst.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;

@Controller
public class AccountController {
    @Autowired
    @Qualifier("userServiceSsmImpl")
    private UserService userService;
    @Qualifier("testPersonnelSsmServiceImpl")
    @Autowired
    private TestPersonnelService testPersonnelService;

    @PostMapping("/login.action")
    public String login(String login,String password,int menu,HttpSession session,Model model){
        try {
            User user = userService.login(login, password);
            session.setAttribute(Constant.CURRENT_USER, user);
            if (user.getType() == 4) {
                TestPersonnel stu = testPersonnelService.findById(user.getId());
                session.setAttribute(Constant.CURRENT_TESTPERSONNEL,stu);
            }
            if (menu == 0) {
                return "redirect:/frame.jsp";
            } else {
                return "redirect:/index.jsp";
            }

        } catch (ExamException ex) {
            model.addAttribute("login", login);
            return "login";
        }
    }

    @RequestMapping("/logout.action")
    public String logout(HttpSession session) {
        // 使 session 失效
        session.invalidate();
        // 跳转到登陆页面
        return "redirect:/login.jsp";
    }

}
